<?php
declare(strict_types=1);
 

namespace App\Game\Mapper;

use App\Game\Model\GameProfit;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 用户收益Mapper类
 */
class GameProfitMapper extends AbstractMapper
{
    /**
     * @var GameProfit
     */
    public $model;

    public function assignModel()
    {
        $this->model = GameProfit::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        // 用户id
        if (isset($params['user_id']) && filled($params['user_id'])) {
            $query->where('user_id', '=', $params['user_id']);
        }
        // 结束时间
        if (isset($params['end_at']) && filled($params['end_at']) && is_array($params['end_at']) && count($params['end_at']) == 2) {
            $query->whereBetween(
                'end_at',
                [ $params['end_at'][0], $params['end_at'][1] ]
            );
        }

        return $query->orderByDesc('id');
    }
}